package com.genius.mobile_page.pandian.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.FillAddJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.dingding.util.GeniusConfig;
import com.genius.mobile_page.common.ErrorResponseData;
import com.genius.mobile_page.common.ResponseData;
import com.genius.mobile_page.common.SuccessResponseData;
import com.genius.mobile_page.pandian.pandianUtil;
import com.genius.mobile_page.pandian.service.PandianEveryService;
import com.genius.mobile_page.pandian.service.PandianService;
import com.genius.mobile_page.pandian_jd.pandianjdUtil;
import com.genius.mobile_page.pingjia.PingjiaUtil;
import com.genius.mobile_page.pingjia.service.PingjiaServiceAdapter;
import com.genius.unite_platform.meeting_info.entity.Meeting_infoEntity;
import com.genius.unite_platform.meeting_info_detail.entity.Meeting_info_detailEntity;
import com.genius.unite_platform.notice_info.entity.Notice_infoEntity;
import com.genius.unite_platform.pandian_item_bipan.entity.Pandian_item_bipanEntity;
import com.genius.unite_platform.work_plan_info.entity.Work_plan_infoEntity;
import com.genius.workflow.element.SQLBean;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.page.SearchResult;
import com.genius.workflow.page.SqlLang;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;
import com.genius.workflow.util.GetNo;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSON;

@Service("pandianEveryService")
public class PandianEveryServiceImpl implements PandianEveryService{


	@Override
	public ResponseData getPandianJiancha(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select a.*,(select name from dict_info_new where"
				+ " type = 'jinhupandian' and id = a.flag)flagname"
				+ ",(select name from t_user where userId = a.xgr)xgrname from pandian_jiancha a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("id"))){
				sql.append(" and id = ?");
				params.add(parms.getString("id"));
			}else{
				if(ObjectUtil.isNotEmpty(parms.getString("pandianDate"))){
					sql.append(" and pandianDate = ?");
					params.add(parms.getString("pandianDate"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
					sql.append(" and flag in ("+parms.getString("flag")+")");
//					params.add(parms.getString("flag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
					sql.append(" and shopcode = ?");
					params.add(parms.getString("shopcode"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("notFlag"))){
					sql.append(" and flag != ?");
					params.add(parms.getString("notFlag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("pingjiaFlag"))){
					sql.append(" and pingjiaFlag = ?");
					params.add(parms.getString("pingjiaFlag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("startDate"))){
					sql.append(" and pandianDate >= ?");
					params.add(parms.getString("startDate"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("endDate"))){
					sql.append(" and pandianDate <= ?");
					params.add(parms.getString("endDate"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("searchValue"))){
					sql.append(" and (shopcode like ? or shopname like ?)");
					params.add("%"+parms.getString("searchValue")+"%");
					params.add("%"+parms.getString("searchValue")+"%");
				}
				if(ObjectUtil.isNotEmpty(parms.getString("job"))){
					sql.append(" and s"+parms.getString("job")+"No = ?");
					params.add(parms.getString("jobnumber"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("wanchengUser"))){
					sql.append(" and wanchengUser = ?");
					params.add(parms.getString("wanchengUser"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("querenFlag"))){
					sql.append(" and querenFlag = ?");
					params.add(parms.getString("querenFlag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("querenUser"))){
					sql.append(" and querenUser = ?");
					params.add(parms.getString("querenUser"));
				}
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null && result.size() > 0){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点检查的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}
	
	@Override
	public ResponseData updatePandianJiancha(JSONObject parms) {
		try {
			
			StringBuilder updateField = new StringBuilder("querenFlag,flag");
			//待确认状态
			if(ObjectUtil.equals("0", parms.getString("querenFlag"))){
				parms.put("querenUser", GeniusConfig.PANDIANQUERENUSER);
				if(ObjectUtil.isNotEmpty(parms.getString("level"))){
					updateField.append(",level");
				}
				updateField.append(",querenUser,sendDate=now(),wanchengUser,wanchengUsername,wanchengDate=now()");
			}else if(ObjectUtil.equals("2", parms.getString("querenFlag"))){
				updateField.append(",querenRemark,querenDate=now()");
				if(parms.getJSONArray("wentiArray").size() > 0){
					DBTools2.updateBatch("shenpiFlag,shenpiDate=now()", "id", "pandian_jiancha_wentifankui", parms.getJSONArray("wentiArray"));
				}
			}else if(ObjectUtil.equals("1", parms.getString("querenFlag"))){
				updateField.append(",querenRemark,querenDate=now()");
				//同意判断问题反馈是否有修改
				if(parms.getJSONArray("wentiArray").size() > 0){
					DBTools2.updateBatch("wenti,senduser,sendusername,shenpiFlag,shenpiDate=now()", "id"
							, "pandian_jiancha_wentifankui", parms.getJSONArray("wentiArray"));
//					DBTools2.updateBatch("wenti,huifuuser,huifuusername,shenpiFlag", "id", "pandian_jiancha_wentifankui", parms.getJSONArray("wentiArray"));
				}
			}
			
			
			DBTools2.update(updateField.toString(), "id", "pandian_jiancha", parms);
			//待确认状态
			if(ObjectUtil.equals("0", parms.getString("querenFlag"))){
				//发送确认通知
				pandianUtil.sendQuerenTongzhi(parms.getString("querenUser")
						, parms.getString("id")
						, parms.getString("shopcode"), parms.getString("shopname"));
			}else if(ObjectUtil.equals("1", parms.getString("querenFlag"))){
				//发送督导总结、盘点回复通知
				pandianUtil.insertPandianJianchaWentifankui(parms.getString("level"),parms.getString("id"));
				//问题反馈
				String huifuUser = parms.getString("huifuUser");
				if(ObjectUtil.isNotEmpty(huifuUser)){
//					JSONObject huifuJson = DBTools2.select("select group_concat(senduser)huifuUser from pandian_jiancha_wentifankui"
//							+ " where pandianid=?", new String[]{parms.getString("id")});
//					huifuUser = huifuJson.getString("huifuUser");
//					//发送问题反馈回复通知
					pandianUtil.sendFankuiTongzhi(huifuUser, parms.getString("id")
							, parms.getString("shopcode"), parms.getString("shopname"));
//					System.out.println(huifuUser);
//					huifuJson = null;
				}
			}
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("更新盘点检查失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}
	
	@Override
	public ResponseData getPandianJianchaInfo(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select s1.daibannum,s2.bipannum from "
				+ "(select count(id)daibannum from pandian_shop_daiban a where flag = 0) s1"
				+ ",(select count(id)bipannum from pandian_item_bipan a where flag = 0"
				+ " and startdate <= curDate() and enddate >= curDate()) s2");
		try {
			
			JSONObject result = DBUtils.select(sql.toString());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点检查的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianchaFlag(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select if(s1.checkFlag = 1,'1','0') pandiandaibanFlag"
				+ ",if(s2b.bipanitemnum = 0 or s2.bipannum > 0,'1','0') bipanxiangFlag, if(s3.zicainum=0 and s3.zicaizongnum > 0,'0','1') zicaipandianFlag"
				+ ",if(s4.biaozhunnum=1 and wancheng = 1,'1','0') biaozhunjianchaFlag,s4.id biaozhunid"
				+ ",s4.wancheng biaozhunwancheng"
				+ ",if(s5a.changguiall=s5b.changguinum,'1','0') changguipandianFlag,'1' wentifankuiFlag from "
				+ "(select flag,checkFlag from pandian_jiancha_daibanshixiang a where pandianid=?) s1 "
				+ ",(select count(pandianid)bipannum from pandian_jiancha_bipan a where pandianid = ?) s2"
				+ ",(select count(id)bipanitemnum from pandian_item_bipan a where CURDATE() BETWEEN startDate and endDate) s2b"
				+ ",(select count(pandianid)zicainum,(select count(id) from pandian_shop_zicai a where flag = 0"
				+ " and shopcode = ? and CURDATE() BETWEEN startDate and endDate)zicaizongnum"
				+ " from pandian_jiancha_zicai a where pandianid = ? and flag = 2) s3"
				+ ",(select count(id)biaozhunnum,id,wancheng from "+GeniusConfig.XUNDIANDATABASE+".pandian_every"
				+ " a where id = (select biaozhunid from pandian_jiancha where id = ?)) s4"
				+ ",(select count(id)changguiall from pandian_jieguo_biaozhun a where flag = 0) s5a"
				+ ",(select count(pandianid)changguinum from pandian_jiancha_changgui a where pandianid=?) s5b ");
//		System.out.println(sql);
		try {
			String pandianid = parms.getString("pandianid");
			String shopcode = parms.getString("shopcode")==null?"":parms.getString("shopcode");
			JSONObject result = DBUtils.select(sql.toString(),new String[]{pandianid,pandianid,shopcode,pandianid,pandianid,pandianid});
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点检查的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianShopDaibanList(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select a.*,if(a.flag = '1','已完成','未完成')flagname "
				+ "from pandian_jiancha_daibanshixiang a where 1=1");
//		System.out.println(sql);
		try {
			JSONArray params = new JSONArray();
//			params.add(parms.getString("pandianid"));
			if(ObjectUtil.isNotEmpty(parms.getString("pandianDate"))){
				sql.append(" and pandianid in (select id from pandian_jiancha where pandianDate = ? and flag = 3 and shopcode=?)");
				params.add(parms.getString("pandianDate"));
				params.add(parms.getString("shopcode"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("pandianid"))){
				sql.append(" and pandianid = ?");
				params.add(parms.getString("pandianid"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
				sql.append(" and flag = ?");
				params.add(parms.getString("flag"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("checkFlag"))){
				sql.append(" and checkFlag = ?");
				params.add(parms.getString("checkFlag"));
			}
			sql.append(" order by sort");
			
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点前待办事项的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianShopDaiban(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select s1.id itemid,s1.itemname,ifnull(s2.flag,0) flag"
				+ ",if(s2.flag = '1','已完成','未完成')flagname from "
				+ "(select id,itemname,sort from pandian_shop_daiban a where flag = 0) s1"
				+ " left join "
				+ "(select pandianid,shopcode,flag,itemid from pandian_jiancha_daibanshixiang a where pandianid=?) s2 "
				+ " on s1.id = s2.itemid");
//		System.out.println(sql);
		try {
			
			JSONArray result = DBUtils.selectArr(sql.toString(),new String[]{parms.getString("pandianid")});
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点前待办事项的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData editPandianJianShopDaiban(JSONObject parms) {
		try {
			StringBuilder updateField = new StringBuilder();
			
			if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
				updateField.append("shophuifu,shopimg,lrr,lrsj=now(),flag");
				//修改盘点状态为门店待办完成
				JSONObject json = new JSONObject();
				json.put("id", parms.getString("pandianid"));
				json.put("flag", 10);
				DBTools2.update("flag", "id", "pandian_jiancha", json);
			}else if(ObjectUtil.isNotEmpty(parms.getString("checkFlag"))){
				updateField.append("checkimg,checkUser,checkUsername,checkDate=now(),checkFlag,checkValue,checkRemark");
			}
			DBTools2.updateBatch(updateField.toString(), "pandianid,shopcode,itemid"
					, "pandian_jiancha_daibanshixiang", parms.getJSONArray("dataArray"));
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("插入盘点前待办事项失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData replacePandianJianBipan(JSONObject parms) {
		try {
			DBTools.replaceBatch("pandianid,shopcode,itemid,itemname,optionid,optionname,otheroptionid,otheroptionname"
					+ ",remark,imgurl,lrr,lrsj=now()", "pandian_jiancha_bipan", parms.getJSONArray("dataArray"));
			
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("必盘项检查失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianBipan(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_jiancha_bipan a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("pandianid"))){
				sql.append(" and pandianid = ?");
				params.add(parms.getString("pandianid"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("lrr"))){
				sql.append(" and lrr = ?");
				params.add(parms.getString("lrr"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and shopcode = ?");
				params.add(parms.getString("shopcode"));
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点检查的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData replacePandianJianchaChanggui(JSONObject parms) {
		try {
			DBTools.replaceBatch("pandianid,shopcode,biaozhun,biaozhunname,biaozhunvalue"
					+ ",lrr,lrsj=now()", "pandian_jiancha_changgui", parms.getJSONArray("dataArray"));
			
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("常规盘点保存失败失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianchaChanggui(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_jiancha_changgui a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("pandianid"))){
				sql.append(" and pandianid = ?");
				params.add(parms.getString("pandianid"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("lrr"))){
				sql.append(" and lrr = ?");
				params.add(parms.getString("lrr"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and shopcode = ?");
				params.add(parms.getString("shopcode"));
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有常规检查的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianchaWentifankui(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_jiancha_wentifankui a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("id"))){
				sql.append(" and id = ?");
				params.add(parms.getString("id"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("pandianid"))){
				sql.append(" and pandianid = ?");
				params.add(parms.getString("pandianid"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("lrr"))){
				sql.append(" and lrr = ?");
				params.add(parms.getString("lrr"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and shopcode = ?");
				params.add(parms.getString("shopcode"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("huifuuser"))){
				sql.append(" and huifuuser = ?");
				params.add(parms.getString("huifuuser"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("senduser"))){
				sql.append(" and senduser = ?");
				params.add(parms.getString("senduser"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("wanchenguser"))){
				sql.append(" and wanchenguser = ?");
				params.add(parms.getString("wanchenguser"));
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有问题反馈的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData insertPandianJianchaWentifankui(JSONObject parms) {
		try {
			
			DBTools2.insertBatch("pandianid,shopcode,flag,shenpiFlag,wenti,imgurl,senduser,sendusername"
					+ ",lrr,lrsj=now()", "pandian_jiancha_wentifankui", parms.getJSONArray("dataArray"));
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点问题反馈新增失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData updatePandianJianchaWentifankui(JSONObject parms) {
		try {
//			System.out.println(parms);
			DBTools2.updateBatch("wenti,senduser,sendusername,shenpiFlag", "id"
					, "pandian_jiancha_wentifankui", parms.getJSONArray("wentiArray"));
			if(ObjectUtil.equals("0", parms.getString("querenFlag"))){
				updatePandianJiancha(parms);
			}
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点问题反馈更新失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData huifuPandianJianchaWentifankui(JSONObject parms) {
		ResponseData res = null;
		try {
			StringBuilder field = new StringBuilder("huifu,huifuImg,huifuuser,huifuusername,wanchenguser,wanchengusername,flag,huifudate=now()");
			if(ObjectUtil.equals("1", parms.getString("flag"))){
				field.append(",wanchengDate=now(),planDate=now()");
			}else{
				field.append(",planDate");
			}
			
			DBTools2.update(field.toString(),"id", "pandian_jiancha_wentifankui", parms);
			//只有从未回复状态点击时才执行
			if(ObjectUtil.equals("huifu", parms.getString("ope"))){
				//更改状态并插入待评价信息
				res = pandianUtil.changePandianDaipingjia(parms);
				pandianUtil.sendFankuiWanchengTongzhi(parms.getString("wanchengUser"), parms.getString("pandianid")
						, parms.getString("shopcode"), null);
			}else{
				res = new SuccessResponseData();
			}
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			res = new ErrorResponseData("盘点问题反馈回复失败");
		} catch (Exception e) {
			e.printStackTrace();
			res = new ErrorResponseData(e.getMessage());
		}
		return res;
	}

	@Override
	public ResponseData getPandianPingjiaItem(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_pingjia_item a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("id"))){
				sql.append(" and id = ?");
				params.add(parms.getString("id"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
				sql.append(" and flag = ?");
				params.add(parms.getString("flag"));
			}
			sql.append(" order by sort");
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点评价项的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianchaPingjia(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_jiancha_pingjia a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("pandianid"))){
				sql.append(" and pandianid = ?");
				params.add(parms.getString("pandianid"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and shopcode = ?");
				params.add(parms.getString("shopcode"));
			}
			sql.append(" order by sort");
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点评价的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData replacePandianJianchaPingjia(JSONObject parms) {
		try {
			JSONArray dataArray = parms.getJSONArray("dataArray");
			DBTools.replaceBatch("pandianid,shopcode,pingjiaitem,pingjiaitemname,pingjiaitemscore,pingjiascore,pingjiaremark"
					+ ",sort,lrr,lrsj=now()", "pandian_jiancha_pingjia", dataArray);
			
			JSONObject json = new JSONObject();
			json.put("pingjiaFlag", "1");
			json.put("id", dataArray.getJSONObject(0).getString("pandianid"));
			json.put("flag", 8);
			DBTools.update("pingjiaFlag", "id", "pandian_jiancha", json);
			
			DBTools2.update("flag", "id", "pandian_jiancha", json);
			
			PingjiaUtil.insertTypePingjia(parms);
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点评价保存失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianShopZicai(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_shop_zicai a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and shopcode = ?");
				params.add(parms.getString("shopcode"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
				sql.append(" and flag = ?");
				params.add(parms.getString("flag"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("gdsincode"))){
				sql.append(" and gdsincode = ?");
				params.add(parms.getString("gdsincode"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("pandianDate"))){
				sql.append(" and startDate <= ? and endDate >= ?");
				params.add(parms.getString("pandianDate"));
				params.add(parms.getString("pandianDate"));
			}
			sql.append(" order by sort");
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有此门店资材的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianchaZicai(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select a.*,case flag when '1' then '进行中'"
				+ " when '2' then '已完成' when '3' then '盘点撤销' else '未开始' end flagname,"
				+ "(select gdsname from pandian_shop_zicai where shopcode = a.shopcode"
				+ " and gdsincode = a.gdsincode limit 1)gdsname"
				+ " from pandian_jiancha_zicai a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and shopcode = ?");
				params.add(parms.getString("shopcode"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
				sql.append(" and flag = ?");
				params.add(parms.getString("flag"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("gdsincode"))){
				sql.append(" and gdsincode = ?");
				params.add(parms.getString("gdsincode"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("pandianid"))){
				sql.append(" and pandianid = ?");
				params.add(parms.getString("pandianid"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("chayi"))){
				sql.append(" and chayinum != 0");
			}
//			sql.append(" order by sort");
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null && result.size() > 0){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有此门店资材的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData replacePandianJianchaZicai(JSONObject parms) {
		try {
			DBTools.replaceBatch("pandianid,shopcode,gdsincode,sysnum,pandiannum,chayinum"
					+ ",flag,lrr,lrsj=now()", "pandian_jiancha_zicai", parms.getJSONArray("dataArray"));
			
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("资材盘点保存失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianShopZicaiPandianZicai(JSONObject parms) {
		
		try {
			JSONArray params = new JSONArray();
			params.add(parms.getString("shopcode"));
			params.add(parms.getString("pandianDate"));
			params.add(parms.getString("pandianDate"));
			params.add(parms.getString("shopcode"));
			params.add(parms.getString("pandianid"));
//System.out.println(params);
			StringBuilder sql = new StringBuilder("select s1.*,s2.pandianid,s2.pandiannum,s2.chayinum,s2.flag pandianFlag"
					+ ",case s2.flag when '1' then '进行中' when '2' then '已完成' when '3' then '盘点撤销' else '未开始' end pandianFlagname  from "
					+ "(select shopcode,gdsincode,gdsname,unit,sysnum,flag,sort from pandian_shop_zicai where flag = 0"
					+ " and shopcode = ? and startDate <= ? and endDate >= ?)s1"
					+ " left join "
					+ "(select pandianid,shopcode,gdsincode,pandiannum,chayinum,flag from pandian_jiancha_zicai"
					+ " where shopcode = ? and pandianid = ?) s2"
					+ " on s1.shopcode = s2.shopcode and s1.gdsincode = s2.gdsincode order by sort");
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有此门店资材的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData editPandianJianchaZicaiFlag(JSONObject parms) {
		try {
			StringBuilder field = new StringBuilder("flag");
			if(ObjectUtil.equals("3", parms.getString("flag"))){
				field.append(",pandiannum,chayinum");
				parms.put("pandiannum", "");
				parms.put("chayinum", "");
			}
			DBTools2.update(field.toString(), "pandianid", "pandian_jiancha_zicai", parms);
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("资材盘点状态更新失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData insertPandianJianchaBiaozhun(JSONObject parms) {
		Connection conn = null;
		ResponseData response = null;
        try {
        	

        	//打开连接
        	conn = DBUtils.getConnect();
        	DBUtils.beginTransaction(conn);
        	JSONObject dataJson = new JSONObject();
        	String sql ="insert into "+GeniusConfig.XUNDIANDATABASE+".pandian_every (low_answers,dudao, sEmployeeNO, shopcode"
        			+ ", answers, score, poweranswer,fakuananswer, wancheng,lrr,lrsj) "
					+ "values ('"+parms.getString("low_ansjson")+"','"+parms.getString("dudao")+"'"
							+ ",'"+parms.getString("sEmployeeNO")+"','"+parms.getString("shopcode")+"'"
					+ ",'" + parms.getString("answers") + "','" + parms.getString("score") + "'"
							+ ",'"+parms.getString("poweranswer")+"','"+parms.getString("fakuananswer")+"'"
					+ ",'"+parms.getString("wancheng")+"','"+parms.getString("userId")+"',now())";
        	if(DBUtils.commitOne(conn, sql)){
        		String sqlId = "select last_insert_id() id";
				ResultSet rs = DBUtils.query(conn, sqlId);
				if(rs.next()){
					dataJson.put("biaozhunid", rs.getString("id"));
				}
				rs.close();
        	}
        	String pdSql = "update pandian_jiancha set biaozhunid = '"+dataJson.getString("biaozhunid")+"'"
        			+ " where id = '"+parms.getString("pandianid")+"'";
        	DBUtils.commitOne(conn, pdSql);
        	DBUtils.endTransaction(conn);
        	response = new SuccessResponseData(dataJson);
        } catch (SQLException throwables) {
			
			throwables.printStackTrace();
			response = new ErrorResponseData("标准盘点插入失败");
		} catch (Exception e) {
			e.printStackTrace();
			response = new ErrorResponseData(e.getMessage());
        } finally {
        	DBUtils.closeDBResources(conn);
		}
		return response;
	}

	@Override
	public ResponseData getPandianJindu(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select "
				+ "if(s1.flag = 1,'1','0') pandiandaibanFlag"
				+ ",if(s1.flag = 1,'完成','未完成') pandiandaibanFlagname,ifnull(s1.lrsj,'') pandiandaibanDate"
				+ ",s2.jianchaFlag,case s2.jianchaFlag when 1 then '盘点中' when 3 then '未开始' when 10 then '未开始'"
				+ " else '已完成' end jianchaFlagname,ifnull(s2.wanchengDate,'') jianchaDate"
				+ ",ifnull(s2.querenDate,'')querenDate,s2.querenFlag,case s2.querenFlag when 1 then '已审批' "
				+ "when 2 then '审批拒绝' else '未审批' end querenFlagname"
				+ ",if(s3.fankuinum=s3.fankuiwanchengnum,'1','0') fankuiFlag"
				+ ",if(s3.fankuinum=s3.fankuiwanchengnum,'已回复','未回复') fankuiFlagname "
				+ ",if(s3.fankuinum=s3.fankuiwanchengnum,s3.fankuiDate,'') fankuiDate"
				+ ",if(s4.huifuFlag=1,'1','0') huifuFlag,if(s4.huifuFlag=1,'已回复','未回复') huifuFlagname"
				+ ",ifnull(s4.huifudate,'')huifuDate,s4.zongjieFlag,ifnull(s5.lrsj,'')pingjiaDate"
				+ ",if(s4.zongjieFlag=1,'已总结','未总结') zongjieFlagname,ifnull(s4.zongjieDate,'')zongjieDate"
				+ ",if(s5.pingjianum=0,'0','1') pingjiaFlag,if(s5.pingjianum=0,'未评价','已评价') pingjiaFlagname from "
				+ "(select flag,checkFlag,lrsj from pandian_jiancha_daibanshixiang a where pandianid=?) s1 "
				+ ",(select flag jianchaFlag,wanchengDate,querenDate,querenFlag from pandian_jiancha a where id = ?) s2"
				+ ",(select count(pandianid)fankuinum,sum(if(flag =1 or flag = 2,1,0))fankuiwanchengnum"
				+ ",max(huifudate)fankuiDate from pandian_jiancha_wentifankui a where pandianid = ?) s3"
				+ ",(select COUNT(pandianid)num,huifuFlag,huifudate huifuDate,zongjieFlag,zongjieDate from pandian_jiancha_huifu where pandianid = ?) s4"
				+ ",(select count(pandianid)pingjianum,lrsj from pandian_jiancha_pingjia a where pandianid=?) s5 ");
//		System.out.println(sql);
		try {
			String pandianid = parms.getString("pandianid");
			JSONObject result = DBUtils.select(sql.toString(),new String[]{pandianid,pandianid,pandianid,pandianid,pandianid});
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点进度的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianHuifu(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_jiancha_huifu a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			
			if(ObjectUtil.isNotEmpty(parms.getString("pandianid"))){
				sql.append(" and pandianid = ?");
				params.add(parms.getString("pandianid"));
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null && result.size() > 0){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点回复的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData huifuPandian(JSONObject parms) {
		ResponseData res = null;
		try {
			
			DBTools2.update("huifuFlag,huifuremark,huifudate=now()","pandianid", "pandian_jiancha_huifu", parms);
			
//			JSONObject json = new JSONObject();
//			parms.put("id", parms.getString("pandianid"));
//			parms.put("flag", 7);
//			DBTools2.update("flag", "id", "pandian_jiancha", parms);
//			
//			parms.put("pingjiaType", "1");
//			
//			//插入待评价信息
//			PingjiaUtil.insertTypePingjia(parms);
			//更改状态并插入待评价信息
			res = pandianUtil.changePandianDaipingjia(parms);
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			res = new ErrorResponseData("盘点回复失败");
		} catch (Exception e) {
			e.printStackTrace();
			res = new ErrorResponseData(e.getMessage());
		}
		return res;
	}

	@Override
	public ResponseData zongjiePandian(JSONObject parms) {
		try {
			
			DBTools2.update("zongjieFlag,zongjiecontent,zongjieDate=now()","pandianid", "pandian_jiancha_huifu", parms);
			//总结成功后发送钉钉回复通知
//			if(ObjectUtil.isNotEmpty(parms.getString("level"))){
//				pandianUtil.insertPandianJianchaWentifankui(parms.getString("level"), parms.getString("pandianid"));
//				String huifuUser = parms.getString("huifuUser");
//				if(ObjectUtil.isEmpty(huifuUser)){
//					JSONObject huifuJson = DBTools2.select("select group_concat(senduser)huifuUser from pandian_jiancha_wentifankui"
//							+ " where pandianid=?", new String[]{parms.getString("pandianid")});
//					huifuUser = huifuJson.getString("huifuUser");
//				}
//				pandianUtil.sendFankuiTongzhi(huifuUser, parms.getString("pandianid")
//						, parms.getString("shopcode"), parms.getString("shopname"));
//			}
//			JSONObject json = new JSONObject();
//			json.put("id", parms.getString("pandianid"));
//			json.put("flag", 6);
//			DBTools2.update("flag", "id", "pandian_jiancha", json);
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点总结失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData pandianDaihuifu(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select a.*,(select name from dict_info_new where"
				+ " type = 'jinhupandian' and id = a.flag) flagname"
				+ ",(select name from t_user where userId = a.xgr)xgrname from pandian_jiancha a where"
				+ " id in (select pandianid from pandian_jiancha_huifu where 1=1 ");
//		System.out.println(sql);
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("huifuuser"))){
				sql.append(" and huifuuser = ?");
				params.add(parms.getString("huifuuser"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("huifuuserjobnumber"))){
				sql.append(" and left(huifuuserjobnumber,6) = ?");
				params.add(parms.getString("huifuuserjobnumber"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("huifuFlag"))){
				sql.append(" and huifuFlag = ?");
				params.add(parms.getString("huifuFlag"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("zongjieFlag"))){
				sql.append(" and zongjieFlag = ?");
				params.add(parms.getString("zongjieFlag"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("zongjieuser"))){
				sql.append(" and zongjieuser = ?");
				params.add(parms.getString("zongjieuser"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("zongjieuserjobnumber"))){
				sql.append(" and zongjieuserjobnumber = ?");
				params.add(parms.getString("zongjieuserjobnumber"));
			}
			sql.append(")");
//			System.out.println(params);
//			System.out.println(sql);
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null && result.size() > 0){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点待回复的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData FankuiDaihuifu(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select a.*,case flag when 1 then '盘点中' "
				+ "when 2 then '已完成' else '未开始' end flagname"
				+ ",(select name from t_user where userId = a.xgr)xgrname from pandian_jiancha a ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("querenFlag"))){
				sql.append(" where querenFlag = ?");
				params.add(parms.getString("querenFlag"));
				if(ObjectUtil.isNotEmpty(parms.getString("wanchengUser"))){
					sql.append(" and wanchengUser = ?");
					params.add(parms.getString("wanchengUser"));
				}
			}else{
				sql.append(" where id in (select pandianid from pandian_jiancha_wentifankui where 1=1");
				if(ObjectUtil.isNotEmpty(parms.getString("huifuuser"))){
					sql.append(" and huifuuser = ?");
					params.add(parms.getString("huifuuser"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
					sql.append(" and flag in ("+parms.getString("flag")+")");
				}
				if(ObjectUtil.isNotEmpty(parms.getString("senduser"))){
					sql.append(" and senduser = ?");
					params.add(parms.getString("senduser"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("shenpiFlag"))){
					sql.append(" and shenpiFlag = ?");
					params.add(parms.getString("shenpiFlag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("lrr"))){
					sql.append(" and lrr = ?");
					params.add(parms.getString("lrr"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("wanchenguser"))){
					sql.append(" and wanchenguser = ?");
					params.add(parms.getString("wanchenguser"));
				}
				sql.append(")");
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null && result.size() > 0){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有反馈待回复的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getBuhegeBiaozhun(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from "+GeniusConfig.XUNDIANDATABASE+".pandian_buhege"
				+ " a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			
			if(ObjectUtil.isNotEmpty(parms.getString("everyId"))){
				sql.append(" and everyId = ?");
				params.add(parms.getString("everyId"));
			}
			
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("标准盘点没有不合格的条款");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianJianchaDaodian(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_jiancha_daodian a where 1=1 ");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("pandianid"))){
				sql.append(" and pandianid = ?");
				params.add(parms.getString("pandianid"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and shopcode = ?");
				params.add(parms.getString("shopcode"));
			}
			if(ObjectUtil.isNotEmpty(parms.getString("lrr"))){
				sql.append(" and lrr = ?");
				params.add(parms.getString("lrr"));
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有此门店到店的信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData replacePandianJianchaDaodian(JSONObject parms) {
		try {
			DBTools.replace("pandianid,shopcode,shopname,shoplatitude,shoplongitude,shopaddress,latitude,longitude"
					+ ",address,img,lrr,lrsj=now()", "pandian_jiancha_daodian", parms);
			JSONObject json = new JSONObject();
			json.put("id", parms.getString("pandianid"));
			json.put("flag", 1);
			DBTools2.update("flag", "id", "pandian_jiancha", json);
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点到店签到失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getShopWeizhi(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select s1.*,s2.id,s2.flag,s2.shopname,s2.sEmployeeNO,s2.sEmployeeName,s2.pandianDate from "
	            		+ "(select shopcode,latitude,longitude,address,(abs(latitude-?)+abs(longitude-?)) cha"
	            		+ " from "+GeniusConfig.XUNDIANDATABASE+".shop_jingwei a) s1 "
	            		+ " right join "
	            		+ " (select * from pandian_jiancha where pandianDate = ?");
		try {
			JSONArray params = new JSONArray();
			params.add(parms.getString("latitude"));
			params.add(parms.getString("longitude"));
			params.add(parms.getString("pandianDate"));

			if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
				sql.append(" and flag in ("+parms.getString("flag")+")");
			}
			if(ObjectUtil.isNotEmpty(parms.getString("searchValue"))){
				sql.append(" and (shopcode like ? or shopname like ?)");
				params.add("%"+parms.getString("searchValue")+"%");
				params.add("%"+parms.getString("searchValue")+"%");
			}
			sql.append(") s2 on s1.shopcode =s2.shopcode");
			if(ObjectUtil.isEmpty("nolimit")){
				sql.append(" order by cha limit 5");
			}
//			System.out.println(sql);
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null && result.size() > 0){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("附近没有需要盘点的门店信息");
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData insertPandianJianchajinji(JSONObject parms) {
		ResponseData resData;
		try {
			String keywords = "shopcode,pandianDate";
	    	String insertTable = "pandian_jiancha";
			JSONObject existJson = DBTools2.getIsExist(keywords,insertTable, parms);
			
			if(existJson != null){
				resData = new ErrorResponseData("此门店已经存在，不能增加紧急盘点");
			}else{
				LinkedList<SQLBean> sqlList = new LinkedList<SQLBean>();
				String pandianSql = "insert into "+insertTable+"(pandianDate,shopcode,shopname,flag"
						+ ",pingjiaFlag,lrr,lrsj,xgr,xgsj,sEmployeeNO,sEmployeeName,sZoneMagNO,sZoneMagName"
						+ ",sDirectorNO,sDirectorName,jinji) "
						+ "SELECT CURDATE() pandianDate,s1.shopcode,s1.shopname,3 flag,0 pingjiaFlag"
						+ ",?,now() lrsj,?,now() xgsj,s2.sEmployeeNO,s2.sEmployeeName,s2.sZoneMagNO"
						+ ",s2.sZoneMagName"
						+ ",s2.sDirectorNO,s2.sDirectorName,1 from"
						+ "(select deptcode shopcode,deptname shopname from "+GeniusConfig.CAIJIDATABASE+".dict_department"
						+ " where type = 1 and deptcode = ?) s1"
						+ "	left join"
						+ "	(select * from "+GeniusConfig.ZHIBIAODATABASE+".dict_tuserstorehistory where sStoreNO = ?) s2"
						+ " on s1.shopcode = s2.sStoreNO";
				String[] pandianParms = new String[]{parms.getString("lrr"),parms.getString("lrr")
						,parms.getString("shopcode"),parms.getString("shopcode")};
				SQLBean pandianSqlBean = new SQLBean(pandianSql, pandianParms);
				sqlList.add(pandianSqlBean);
				
				String daibanSql = "REPLACE into pandian_jiancha_daibanshixiang(pandianid,shopcode,itemid,itemname,flag,checkFlag,sort)"
					+" SELECT s1.id,s1.shopcode,s2.id itemid,s2.itemname,0,0,s2.sort from"
					+" (SELECT id,pandianDate,shopcode,shopname from "+insertTable
					+" WHERE CURDATE() = pandianDate and flag = 3 and shopcode = ?) s1,"
					+" (SELECT * from pandian_shop_daiban WHERE flag = 0) s2";
				String[] daibanParms = new String[]{parms.getString("shopcode")};
				SQLBean daibanSqlBean = new SQLBean(daibanSql, daibanParms);
				sqlList.add(daibanSqlBean);
				
				DBTools2.executeBatch(sqlList);
				resData = new SuccessResponseData();
			}
			
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			resData = new ErrorResponseData("紧急盘点增加失败");
		} catch (Exception e) {
			e.printStackTrace();
			resData = new ErrorResponseData(e.getMessage());
		}
		return resData;
	}

//	@Override
//	public ResponseData insertPandianPlan(JSONObject parms,HttpSession session) {
//		
//		try {
//			String userId = parms.getString("userId");
//			if(ObjectUtil.isEmpty(userId)){
//				userId = session.getAttribute("userId")+"";
//			}
//			DBTools2.insertBatch("pandianDate,shopcode,shopname,flag,lrr='"+userId+"',lrsj=now()"
//					+ ",xgr='"+userId+"',xgsj=now()", "pandian_plan", parms.getJSONArray("shopList"));
//			return new SuccessResponseData();
//			
//			
//		} catch (SQLException throwables) {
//			
//			throwables.printStackTrace();
//			return new ErrorResponseData("盘点计划新增失败");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return new ErrorResponseData(e.getMessage());
//		}
//	}
//
//	@Override
//	public ResponseData editPandianPlan(JSONObject parms, HttpSession session) {
//		try {
//			String userId = parms.getString("userId");
//			if(ObjectUtil.isEmpty(userId)){
//				userId = session.getAttribute("userId")+"";
//			}
//			DBTools2.update("pandianDate,shopcode,shopname,flag,xgr='"+userId+"',xgsj=now()","id", "pandian_plan", parms);
//			
//			return new SuccessResponseData();
//			
//		} catch (SQLException throwables) {
//			
//			throwables.printStackTrace();
//			return new ErrorResponseData("盘点计划编辑失败");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return new ErrorResponseData(e.getMessage());
//		}
//	}
//
//	@Override
//	public ResponseData deletePandianPlan(JSONObject parms) {
//		try {
//			DBTools2.delete("id", "pandian_plan", parms);
//			return new SuccessResponseData();
//		} catch (SQLException throwables) {
//			
//			throwables.printStackTrace();
//			return new ErrorResponseData("盘点计划删除失败");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return new ErrorResponseData(e.getMessage());
//		}
//	}

	
	
	
}

